## ---------------------------
##
## Script name: process_comp_disadv_Chicago.R
##
## Description: Cleans and formats the raw male incarceration 
## file.
##
## ---------------------------
## DEPENDENCIES

library(dplyr)
library(tidyverse)
library(ggplot2)
library(stringr)
library(conflicted)

conflict_prefer("filter", "dplyr")

## ---------------------------


# WRAPPER FUNCTION
process_comp_disadv_Chicago <- function(file_locations, overwrite=F) {
  #'
  #'@description Wrapper function that calls a functions to
  #'process Chicago male incarceration data.
  #'
  #'@param file_locations list. File locations loaded from
  #'file_locations.R
  #'
  #'@param overwrite boolean. Determines if already processed 
  #'file should be overwritten.
  #'
  #'@return NULL

  # this process is done for Chicago with the other cities in process_comp_disadv_Nation.R
  # .process_police_shootings_chicago(file_locations, overwrite=overwrite)
  
  # Call the function to process male incarceration file
  .process_male_incarceration_chicago(file_locations, overwrite=overwrite)
  
}

.process_male_incarceration_chicago <- function(file_locations, overwrite=F) {
  #'
  #'@description Renames column names in male incarceration
  #'file.
  #'
  #'@param file_locations list. File locations loaded from
  #'file_locations.R
  #'
  #'@param overwrite boolean. Determines if already processed 
  #'file should be overwritten.
  #'
  #'@return NULL

  output_fp <- paste(file_locations$current_fp, file_locations$Chicago$`Male Incarceration`$processed, sep="/")
  
  if ((!file.exists(output_fp)) | overwrite) {
    df_inc <- read.csv(paste(file_locations$current_fp, file_locations$Chicago$`Male Incarceration`$raw, sep="/")) %>%
      mutate(tract_2010 = str_pad(tract, 11, side="left", pad="0"),
             year = 2010) %>%
      rename(incarc_rate = Incarceration_Rate_rP_gM_pall) %>%
      select(-Name, -tract) %>%
      mutate(incarc_rate = incarc_rate*10000,
             city = "Chicago")
    
    write.csv(df_inc, output_fp,
            row.names = F)
  }
  
}





